package movieGandalf.BL;

import java.math.BigInteger;
import java.sql.Connection;
import java.util.List;

import dataObjects.UserImp;

import movieGandalf.logicGuiConnection.User;
import movieGandalf.database.MovieTools;
import movieGandalf.logicGuiConnection.Movie;
import movieGandalf.search.SearchParam;

public class ObjectsMYSQLBL implements ObjectsBL {
	private static Connection conn = MovieTools.getConn();
	private static User user;

	
	public ObjectsMYSQLBL(){
		conn = MovieTools.getConn();
	}
	
//	public boolean LoginUser(String username, String password) {
//		User user = (User) MovieTools.getUserByName(conn, username);
//		if (user == null)
//			return false;
//		if (!user.getPassword().contentEquals(password))
//			return false;
//		this.user = user;
//		return true;
//	}

	@Override
	public void LogoutUser() {
		user = null;
	}

	public boolean registerUser(String username, String password) {
		User tmpUser = MovieTools.getUserByName(conn,username);
		BigInteger ID = null; 
		if (tmpUser != null)
			return false;
		User newUser = (User) new UserImp(ID,username,password,false);
		if(newUser==null)
			return false;
		user=newUser;
		newUser.update();
		return true;
	}

	@Override
	public List<Movie> findMovie(List<SearchParam> searchParams) {
		return MovieTools.getMoviesByFilter(conn, searchParams);
	}

	@Override
	public void addToAlreadySeen(BigInteger movieID) {
		MovieTools.addAlreadySeen(this.conn, user.getID(), movieID);
	}

	@Override
	public void addToWishList(BigInteger movieID) {
		MovieTools.addWishList(this.conn, user.getID(), movieID);
	}

	@Override
	public void addToMustSee(BigInteger movieID) {
		MovieTools.addMustSee(this.conn, user.getID(), movieID);
	}

	@Override
	public void deleteFromAlreadySeen(BigInteger movieID) {
		MovieTools.deleteAlreadySeen(this.conn, user.getID(), movieID);
	}

	@Override
	public void deleteFromWishList(BigInteger movieID) {
		MovieTools.deleteWishList(this.conn, user.getID(), movieID);
	}

	@Override
	public void deleteFromMustSee(BigInteger movieID) {
		MovieTools.addMustSee(this.conn, user.getID(), movieID);
	}

	@Override
	public Connection getConnection() {
		return conn;
	}

	@Override
	public boolean Login(User user, String password) {
		if (user == null)
			return false;
		if (!user.getPassword().equals(password))
			return false;
		this.user = user;
		return true;

	}
	@Override
	public Movie getMovie(BigInteger movieID){
		return MovieTools.getMovie(conn, movieID);
	}

}
